import {QUESTIONS,QUESTION_DETAIL,QUESTION_ANSWER} from '../types/question'
import {createAction} from 'redux-actions'
import {service} from '../../service/api.js'
import {$post} from '../../util/request'
import {getType} from '../../util/index'

export const getQuestionId = createAction(QUESTIONS, async(param) => {
  let res = await $post(service.getQuestionIds, param)
  const list = {
    singleSelect:[],
    multiSelect:[],
    judges:[],
    count:0
  }
  res.forEach(item=>{
    list.count++
    list[getType(item.CECollectionId)].push(item)
  })

  return list
})

export const getQuestionDetailById = createAction(QUESTION_DETAIL, (id) => {
  return $post(service.getQuestionById, {questionid:id})
})

export const getQuestionCard = createAction(QUESTION_ANSWER, async(param={}) => {
  let res =  await $post(service.getAnswerCard, {...param})
  const list = {
    singleSelect:[],
    multiSelect:[],
    judges:[]
  }
  res.forEach(item=>{
    let a = item.userAnswer?(item.ceAnswer==item.userAnswer?'correct':((item.ceAnswer!=item.userAnswer&&item.ceAnswer.indexOf(item.userAnswer) == -1)?'fault':'partCorrect')):'';
    if(item.ceCollectionId==14){
      item.className=a
    }
    list[getType(item.ceCollectionId)].push(item)
  })
  list.singleSelect[0].userAnswer='A'
  list.multiSelect[0].userAnswer='A'
  list.judges[0].userAnswer='A'
  return list
})

